package com.cnhis.cloudhealth.clinical.infusion.base.dao;

import com.cnhis.cloudhealth.commons.dao.BaseDao;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hdd on 2016/9/18.
 */
@Repository
public class InfusionDao extends BaseDao {
    /**
     * 查询病人列表信息 （输液待执行病人）
     *
     * @param map
     * @return
     * @throws Exception
     */
    public Map<String, Object> findPatientList(Map<String, Object> map) throws Exception {
        List list = this.queryForList("InfusionMapper.findPatientList", map);
        Map<String, Object> infomap = new HashMap<String, Object>();
        infomap.put("list", list);
        return infomap;
    }


    /**
     * 查询病人列表信息 （输液 已完成  病人）
     *
     * @param map
     * @return
     * @throws Exception
     */
    public Map<String, Object> findCompletedPatientList(Map<String, Object> map) throws Exception {
        List list = this.queryForList("InfusionMapper.findCompletedPatientList", map);
        Map<String, Object> infomap = new HashMap<String, Object>();
        infomap.put("list", list);
        return infomap;
    }

    /**
     * 查询病区科室
     *
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryDept(Map<String, Object> map) throws Exception {
        List list = this.queryForList("InfusionMapper.queryDept", map);
        Map<String, Object> depmap = new HashMap<String, Object>();
        depmap.put("list", list);
        return depmap;
    }


    /**
     * 根据 就诊id  查询此病人的详细信息
     *
     * @param map
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryDetailPatient(Map<String, Object> map) throws Exception {
        List list = this.getSqlSession().selectList("InfusionMapper.queryDetailPatient", map);
        Map<String, Object> detmap = new HashMap<String, Object>();
        detmap.put("list", list);
        return detmap;
    }

    /**
     * 获取病人  执行项目
     * 待执行病人  （进行中的 项目）
     *
     * @param map
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryExecutePro(Map<String, Object> map, int currPage, int pageSize) throws Exception {
        return queryForPage("InfusionMapper.queryExecutePro", map, currPage, pageSize);
    }

    /**
     * queryOverProject  2016-12-16日  开始使用
     * 获取病人  已完成项目
     * 待执行病人  （已完成项目的 项目）
     *
     * @param map
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryOverProject(Map<String, Object> map, int currPage, int pageSize) throws Exception {
        return queryForPage("InfusionMapper.queryOverProject", map, currPage, pageSize);
    }

    /**
     * 获取病人  执行项目  2016-12-16日  停止使用
     * 已完成  病人  （已完成的 项目）
     *
     * @param map
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryCompleteExecutePro(Map<String, Object> map, int currPage, int pageSize) throws Exception {
        return queryForPage("InfusionMapper.queryCompleteExecutePro", map, currPage, pageSize);
    }

    /**
     * 获取病人  执行记录   也就是项目的明细
     *
     * @param map
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryExecuteRecord(Map<String, Object> map, int currPage, int pageSize) throws Exception {
        return queryForPage("InfusionMapper.queryExecuteRecord", map, currPage, pageSize);
    }


    /**
     * 获取收费 记录
     *
     * @param map
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryChargeRecord(Map<String, Object> map, int currPage, int pageSize) throws Exception {
        return queryForPage("InfusionMapper.queryChargeRecord", map, currPage, pageSize);
    }

    /**
     * 查询所有  待执行医嘱  信息
     *
     * @param map
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryExcuseDoctor(Map<String, Object> map, int currPage, int pageSize) throws Exception {
        return queryForPage("InfusionMapper.queryExcuseDoctor", map, currPage, pageSize);
    }

    /**
     * 接单时  查询搜索的人员
     * 执行人  配药人  接单人
     *
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryUser(Map<String, Object> paraMap) throws Exception {
        List list = this.queryForList("InfusionMapper.queryUser", paraMap);
        Map<String, Object> usermap = new HashMap<String, Object>();
        usermap.put("list", list);
        return usermap;
    }

    /**
     * 查询  输液单，注射单 等 各个单据  用于打印
     *
     * @return
     * @throws Exception
     */
    public Map<String, Object> querySingleinfusion(Map<String, Object> paramMap) throws Exception {
        List list = this.queryForList("InfusionMapper.querySingleinfusion", paramMap);
        Map<String, Object> singlemap = new HashMap<String, Object>();
        singlemap.put("list", list);
        return singlemap;
    }

    /**
     * 根据 vaf_list 的值 查询视图 V_VAF_1
     * 循环 判断  vaf10 是否有小于8 的
     */
    public List searchVafView(List<Map<String, Object>> list) throws Exception {
        List viewlist = this.queryForList("InfusionMapper.searchVafView", list);
        return viewlist;
    }


    public Map<String, Object> queryForPage(String sqlDef, Map<String, Object> param, int currPage, int pageSize) throws Exception {
        String getCountSqlDef = sqlDef + "Count";
        /*获取查询结果的记录总数*/
        int total = ((Integer) this.getSqlSession().selectOne(getCountSqlDef, param)).intValue();
        int pageIndex = currPage - 1;
        pageIndex = pageIndex < 0 ? 0 : pageIndex;
        int startNum1 = pageIndex * (pageSize < 1 ? 1 : pageSize);
        List list = this.getSqlSession().selectList(sqlDef, param, new RowBounds(startNum1, pageSize));
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("list", list);
        map.put("total", total);
        return map;
    }


    /**
     * 点击执行完成 的操作
     *
     * @param list
     * @return
     * @throws Exception
     */
    public String performOver(List<Map<String, Object>> list) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("list", list);
        this.getSqlSession().update("InfusionMapper.performOver", map);
        this.getSqlSession().update("InfusionMapper.performOver2", map);
        return "yes";
    }

    public void completes(Map<String, Object> map) throws Exception {
        this.getSqlSession().update("InfusionMapper.completes", map);
        this.getSqlSession().update("InfusionMapper.completes2", map);
    }


    /**
     * 查询 退费审核 列表
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryRefundAudit(Map<String, Object> paramMap, int currPage, int pageSize) throws Exception {
        return queryForPage("InfusionMapper.queryRefundAudit", paramMap, currPage, pageSize);
    }

    /**
     * 退费审核中的 人员搜索
     *
     * @param paramMap
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryUserInRefundAudit(Map<String, Object> paramMap, int currPage, int pageSize) throws Exception {
        return queryForPage("InfusionMapper.queryUserInRefundAudit", paramMap, currPage, pageSize);
    }

    //保存  修改费用明细
    public void batchUpdateByVaf(Map<String, Object> map) throws Exception {
        this.getSqlSession().update("InfusionMapper.updateVaj", map);
        this.getSqlSession().update("InfusionMapper.updateVaj_2", map);
    }

    public void saveVbj1(Map<String, Object> map) throws Exception {
        this.getSqlSession().insert("InfusionMapper.saveOrdervbj", map);
    }

    /**
     * 查询是否有未交费的
     *
     * @param list
     * @return
     * @throws Exception
     */
    public Map<String, Object> searchChargeCount(List<Map<String, Object>> list) throws Exception {
        List baclist = new ArrayList();
        baclist = getSqlSession().selectList("InfusionMapper.searchChargeCount", list);
        Map<String, Object> backmap = new HashMap<String, Object>();
        backmap.put("list", baclist);
        return backmap;
    }


    /**
     * 修改医嘱状态  修改为进行中
     *
     * @param map
     * @throws Exception
     */
    public void updateVBI1(Map<String, Object> map) throws Exception {
        this.getSqlSession().update("InfusionMapper.updateVBI1", map);
    }

    public void updateVBI1_2(Map<String, Object> map) throws Exception {
        this.getSqlSession().update("InfusionMapper.updateVBI1_2", map);
    }


    /**
     * 根据科室查询  此科室下的所有人员
     * 退费审核
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    public Map<String, Object> refundAuditByDepId(Map<String, Object> paramMap, int currPage, int pageSize) throws Exception {
        return queryForPage("InfusionMapper.refundAuditByDepId", paramMap, currPage, pageSize);
    }

    /**
     * 点击皮试结果
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    public List<Map<String, Object>> skinTest(Map<String, Object> paramMap) throws Exception {
        return getSqlSession().selectList("InfusionMapper.skinTest", paramMap);
    }

    public List<Map<String, Object>> skinTest1(Map<String, Object> paramMap) throws Exception {
        return getSqlSession().selectList("InfusionMapper.skinTest1", paramMap);
    }

    /**
     * 基本信息 保存皮试前使用
     *
     * @param paramMap
     * @return
     */
    public List queryBaseInfo(Map<String, Object> paramMap) throws Exception {
        return getSqlSession().selectList("InfusionMapper.queryBaseInfo", paramMap);
    }

    /**
     * 皮试的修改
     * 保存皮试信息   更新vaf
     *
     * @param paramMap
     * @throws Exception
     */
    public void saveSkinTestResult(Map<String, Object> paramMap) throws Exception {
        getSqlSession().update("InfusionMapper.saveSkinTestResult", paramMap);
    }

    /**
     * 查询是否具有过敏记录
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    public List queryIsHasSkinRecord(Map<String, Object> paramMap) throws Exception {
        return getSqlSession().selectList("InfusionMapper.queryIsHasSkinRecord", paramMap);
    }

    /**
     * 添加过敏记录
     *
     * @param paramMap
     * @throws Exception
     */
    public void addSkinRecord(Map<String, Object> paramMap) throws Exception {
        getSqlSession().insert("InfusionMapper.addSkinRecord", paramMap);
    }

    /**
     * 修改过敏记录
     *
     * @param paramMap
     * @throws Exception
     */
    public void updateSkinRecord(Map<String, Object> paramMap) throws Exception {
        getSqlSession().update("InfusionMapper.updateSkinRecord", paramMap);
    }

    /**
     * 查询是否具有过敏药物
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    public List queryIsHasSkinDrug(Map<String, Object> paramMap) throws Exception {
        return getSqlSession().selectList("InfusionMapper.queryIsHasSkinDrug", paramMap);
    }

    /**
     * 添加过敏药物
     *
     * @param paramMap
     * @throws Exception
     */
    public void addSkinDrug(Map<String, Object> paramMap) throws Exception {
        getSqlSession().insert("InfusionMapper.addSkinDrug", paramMap);
    }

    /**
     * @param '[map]'
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @throws
     * @Description:(病人退费申请)
     */
    public Map<String, Object> findAppRefund(Map map) throws Exception {
        return (Map<String, Object>) this.queryToObject("InfusionMapper.findAppRefund", map);
    }

    /**
     * @param '[map]'
     * @throws
     * @Description:(更新退费状态1)
     */
    public void refund1(Map map) throws Exception {
        update("InfusionMapper.refund1", map);
    }

    /**
     * @param '[map]'
     * @throws
     * @Description:(更新退费状态2)
     */
    public void refund2(Map map) throws Exception {
        update("InfusionMapper.refund2", map);
    }

    /**
     * //判断病人卡上钱是否够用
     *
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryVai23(Map<String, Object> map) throws Exception {
        List list = getSqlSession().selectList("InfusionMapper.queryVai23", map);
        Map<String, Object> backmap = new HashMap<String, Object>();
        backmap.put("list", list);
        return backmap;
    }

    /**
     * @param maps
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @throws
     * @Title: findSumMoney
     * @Description: （查询扣费总金额）
     * @author chengshaowei
     * @date 2017/9/28
     */
    public Map<String, Object> findSumMoney(Map<String, Object> maps) {
        return getSqlSession().selectOne("InfusionMapper.findSumMoney", maps);
    }

    public Map<String, Object> queryVaiAll(Map<String, Object> map) throws Exception {
        List list = getSqlSession().selectList("InfusionMapper.queryVaiAll", map);
        Map<String, Object> backmap = new HashMap<String, Object>();
        backmap.put("alllist", list);
        return backmap;
    }


    //<!--查询赠送的金额-->
    public Map<String, Object> queryCountByVaa01(String vaa01) throws Exception {
        return getSqlSession().selectOne("InfusionMapper.queryCountByVaa01", vaa01);
    }

    // <!--查询所有药品金额-->
    public Map<String, Object> queryVaj38ByVaa01(String vaa01) throws Exception {
        return getSqlSession().selectOne("InfusionMapper.queryVaj38ByVaa01", vaa01);
    }


    /**
     * 根据 vaf01  查询vbj表  查询出  执行时间 vaj08  用于打印
     *
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryVaj08ByVaf01(Map<String, Object> paramMap) throws Exception {
        return getSqlSession().selectOne("InfusionMapper.queryVaj08ByVaf01", paramMap);
    }

    /**
     * 查询病人 是不是会员
     *
     * @throws Exception
     */
    public Map<String, Object> findMembers(String vaa01) throws Exception {
        return getSqlSession().selectOne("InfusionMapper.findMembers", vaa01);
    }

    /**
     * 查询病人 会员卡类型
     */
    public Map<String, Object> findMemberCardType(String vaa01) throws Exception {
        return getSqlSession().selectOne("InfusionMapper.findMemberCardType", vaa01);
    }

    /**
     * 根据 科室 和  vaf01 查询是否可以申请  是本科室给药途径退费
     *
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryApplyRefuse(Map<String, Object> paramMap) throws Exception {
        return getSqlSession().selectOne("InfusionMapper.queryApplyRefuse", paramMap);
    }

    /**
     * 院内消息查询
     *
     * @param map
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String, Object> yuanNeiNotice(Map<String, Object> map, int currPage, int pageSize) throws Exception {
        return queryForPage("InfusionMapper.yuanNeiNotice", map, currPage, pageSize);
    }

}
